//
//  DBHTabBarView.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/16.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "DBHTabBarView.h"

@interface DBHTabBarView ()

@property (nonatomic, copy) selecIndexBack callBack;

@end

@implementation DBHTabBarView

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (instancetype)initWithNames:(NSArray *)names selectIndex:(selecIndexBack)selectIndex
{
    self = [super init];
    if (self) {
        _callBack = selectIndex;
        for (NSInteger i = 0; i < names.count; i++) {
            UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i * SCREEN_WIDTH * 0.125, 0, SCREEN_WIDTH * 0.125, SCREEN_HEIGHT * 0.09);
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@未点击.png", names[i]]] forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@点击.png", names[i]]] forState:UIControlStateSelected];
            [button addTarget:self action:@selector(respondsToBtn:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = 10 + i;
            [self addSubview:button];
        }
        // 默认选中第一个按钮
        UIButton * button = [self viewWithTag:10];
        button.selected = YES;
        _selectedBtnIndex = button.tag;
    }
    return self;
}

#pragma mark 🌷🌷🌷event response🌷🌷🌷
- (void)respondsToBtn:(UIButton *)sender {
    UIButton * button = [self viewWithTag:_selectedBtnIndex];
    button.selected = NO;
    _selectedBtnIndex = sender.tag;
    sender.selected = YES;
    _callBack(sender.tag - 10);
}

@end
